# -*- coding: utf-8 -*-
# @Time : 2022/7/24 16:50
# @Author : WangYunfei
# @FileName: login.py
# @Email: lxb_wyf@163.com
"""
非对称加密 房天下
"""
import execjs
import requests


class FTX(object):

    def __init__(self):
        self.url = "https://passport.fang.com/login.api"
        self.headers = {
            # "Accept": "*/*",
            # "Accept-Encoding": "gzip, deflate, br",
            # "Accept-Language": "zh-CN,zh;q=0.9",
            # "Connection": "keep-alive",
            # "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            # "Cookie": "city=; global_cookie=dmy8zt2bwpoinwl9dq485anos15l5z2cyim; token=52764e145d384d02b658fcaa227fbb0b; __jsluid_s=fb2a80aece0df5f8c28978f3040091b0; g_sourcepage=txz_dl%5Egg_pc; unique_cookie=U_dmy8zt2bwpoinwl9dq485anos15l5z2cyim*4",
            # "Host": "passport.fang.com",
            "Origin": "https://passport.fang.com",
            "Referer": "https://passport.fang.com/?backurl=https%3A%2F%2Fnewhouse.fang.com%2Fhouse%2Fs%2Fb92%2F",
            # "sec-ch-ua": "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"",
            # "sec-ch-ua-mobile": "?0",
            # "sec-ch-ua-platform": "\"Windows\"",
            # "Sec-Fetch-Dest": "empty",
            # "Sec-Fetch-Mode": "cors",
            # "Sec-Fetch-Site": "same-origin",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
            "X-Requested-With": "XMLHttpRequest"
        }

    def spider(self, uid, pwd):
        _data = {
            "uid": uid,
            "pwd": self.js(pwd),
            "Service": "soufun-passport-web",
            "AutoLogin": "1"
        }
        res = requests.post(url=self.url, data=_data, headers=self.headers)
        print(res.text)

    @staticmethod
    def js(value):
        """
        读取js 加密
        :param value:
        :return:
        """
        with open("login.js", "r") as f:
            js_str = f.read()
        context = execjs.compile(js_str)
        params = context.call("encrypted_str", value)
        return params


if __name__ == '__main__':
    FTX().spider("17639569679", "123456778")






